package pers.vic.upload.socket.model;

import org.springframework.util.Assert;

import java.time.LocalDateTime;

/**
 * 描述: 当前正在上传中的文件信息
 * @author Vic.xu
 * @date 2022-06-09 11:22
 */
public class CurrentUploadTask {

    /**
     * 初始化的指令信息
     */
    private InitCommandInfo commandInfo;
    /**
     * 写入位置
     */
    private long writePosition;
    /**
     * 开始时间
     */
    private LocalDateTime startTime;
    /**
     * 文件信息
     */
    private RealFile realFile;

    public CurrentUploadTask() {
    }

    /**
     * 创建 当前上传中的文件信息
     * @param commandInfo
     * @param realFile
     * @return
     */
    public static CurrentUploadTask newTask(InitCommandInfo commandInfo, RealFile realFile) {
        return resume(commandInfo, 0, realFile);
    }

    /**
     * 恢复断点续传
     * @param commandInfo
     * @param physicalLength 物理长度
     * @param realFile
     * @return
     */
    public static CurrentUploadTask resume(InitCommandInfo commandInfo, long physicalLength, RealFile realFile) {
        CurrentUploadTask task = new CurrentUploadTask();
        task.commandInfo = commandInfo;
        task.writePosition = physicalLength;
        task.startTime = LocalDateTime.now();
        task.realFile = realFile;
        return task;
    }

    /**
     * 增加长度
     * @param payloadLength 当前分片的长度
     */
    public void increasePosition(int payloadLength) {
        Assert.isTrue(payloadLength >= 0, "长度错误");
        this.writePosition += payloadLength;
    }

    public InitCommandInfo getCommandInfo() {
        return commandInfo;
    }

    public void setCommandInfo(InitCommandInfo commandInfo) {
        this.commandInfo = commandInfo;
    }

    public long getWritePosition() {
        return writePosition;
    }

    public void setWritePosition(long writePosition) {
        this.writePosition = writePosition;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public RealFile getRealFile() {
        return realFile;
    }

    public void setRealFile(RealFile realFile) {
        this.realFile = realFile;
    }
}
